﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApplication1.Data.Model;

using System.Data.Entity.Migrations;

namespace WebApplication1.Data
{
    public class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool users = false, bool artwork = false)
        {
            if (users)
            {
                SeedUsers(db);
            }
            if (artwork)
            {
                SeedArtWork(db);
            }
            
        }

        private static void SeedUsers(ApplicationDbContext db)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            if (!db.Users.Any(a => a.UserName == "User1"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "User1",
                    Email = "user1@yahoo.com",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                db.SaveChanges();
            }

            if (!db.Users.Any(a => a.UserName == "User2"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "User2",
                    Email = "user2@yahoo.com",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                db.SaveChanges();
            }

            if (!db.Users.Any(a => a.UserName == "User3"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "User3",
                    Email = "user3@yahoo.com",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                db.SaveChanges();
            }
        }

        private static void SeedArtWork(ApplicationDbContext db)
        {
            string User1Id = db.Users.SingleOrDefault(a => a.UserName == "User1").Id;
            string User2Id = db.Users.SingleOrDefault(a => a.UserName == "User2").Id;
            string User3Id = db.Users.SingleOrDefault(a => a.UserName == "User3").Id;

            db.Artworks.AddOrUpdate(a => a.ArtWorkId,
                 new ArtWork() {ArtWorkId = 1, Title="Ezcuze", Description="Notubakala", UserId=User1Id},
                 new ArtWork() {ArtWorkId = 2, Title="lolzds", Description="frr4rsfdsx", UserId=User1Id},
                 new ArtWork() {ArtWorkId = 3, Title="fdasfd", Description="ffsdfdsfew", UserId=User1Id},
                 new ArtWork() {ArtWorkId = 4, Title="fdsaas", Description="fdsafwvcfd", UserId=User2Id},
                 new ArtWork() {ArtWorkId = 5, Title="ewrfds", Description="fdsafsdere", UserId=User2Id},
                 new ArtWork() {ArtWorkId = 6, Title="xcvdsf", Description="fdsafsafw4", UserId=User2Id},
                 new ArtWork() {ArtWorkId = 7, Title="x4wfss", Description="gfxftrewre", UserId=User3Id},
                 new ArtWork() {ArtWorkId = 8, Title="Ezzuze", Description="fsa4r4wsfd", UserId=User3Id}             
            );
            db.SaveChanges();
        }

        
    }
}
